package com.lc.stock.module.crawldata.impl;

import com.alibaba.fastjson.JSONArray;
import com.lc.stock.common.CrawDataAPI;
import com.lc.stock.module.compyindustryconcept.dao.CompyIndustryConceptDao;
import com.lc.stock.module.compyindustryconcept.domain.CompyIndustryConcept;
import com.lc.stock.module.compyindustryconcept.domain.IndustryConcept;
import com.lc.stock.module.crawldata.AbstractCrawData;
import com.lc.stock.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lichen
 * @description 抓取公司指数、行业、概念。
 * @menu
 * @date 2023/2/7 9:04
 */
@Slf4j
@Service
public class CrawCompyIndustryConceptService extends AbstractCrawData {

    @Autowired
    private HttpClientUtil httpClientUtil;
    @Autowired
    private CompyIndustryConceptDao compyIndustryConceptDao;

    /**
     * 抓取公司指数、行业、概念。
     * @param stockCode
     */
    @Override
    public void doCraw(String stockCode) {
        try{
            String resp = httpClientUtil.get(CrawDataAPI.COMPY_INDUSTRY_CONCEPT.getUrl(), stockCode);
            List<IndustryConcept> industryConcepts = JSONArray.parseArray(resp, IndustryConcept.class);
            CompyIndustryConcept compyIndustryConcept = new CompyIndustryConcept();
            compyIndustryConcept.setStockCode(stockCode);
            compyIndustryConcept.setIndustryConcepts(industryConcepts);

            //保存、更新数据库
            CompyIndustryConcept byStockCode = compyIndustryConceptDao.findByStockCode(stockCode);
            if(null != byStockCode){
                compyIndustryConcept.set_id(byStockCode.get_id());
            }
            compyIndustryConceptDao.save(compyIndustryConcept);
        }catch(Exception e){
            log.error("抓取公司" + stockCode + "指数、行业、概念数据失败",e);
        }
    }

    @Override
    public String getSubClassName() {
        return this.getClass().getName();
    }
}
